package com.fysports.web.controller.app;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import com.fysports.web.controller.AbstractBaseController;
import com.fysports.web.controller.engine.RestFulController;
import com.fysports.web.kit.commons.PathBizKit;
import com.fysports.web.kit.dtgrid.DTGridKit;
import com.fysports.web.kit.dtgrid.DTGridView;
import com.fysports.web.model.app.AppBanner;
import com.fysports.web.module.core.DefaultModuleBuilder;
import com.fysports.web.module.core.IModuleFactory;
import com.fysports.web.module.view.JsonView;
import com.fysports.web.module.view.OperateType;

import java.util.List;
import java.util.Map;

/**
 * App的Banner管理
 * Created by sunk
 * Date : 15/11/16 下午
 * Project : JdmData-Server
 */
public class AppBannerController extends AbstractBaseController implements RestFulController{
    private static Log logger = Log.getLog(AppBannerController.class);
    private IModuleFactory imf = new DefaultModuleBuilder();

    public void index() {
        imf.create(this);
        execute();
        render(PathBizKit.getPageAdminPath("system/appbanner/banner_list.ftl"));
    }

    public void load(){
        DTGridView dtview= DTGridKit.toDtView(getPara(DTGridKit.PARAM_NAME));
        Page<AppBanner> list;
        List<Object> args = Lists.newArrayList();
        String sqlext = " and liveflag = 1"; //TODO 筛选未逻辑删除字段
        //高级查询
        if (DTGridKit.isAdvance(dtview)) {
            sqlext += DTGridKit.getQueryConditionByType(dtview, args, DTGridKit.TYPE_ADVANCEQUERY);
            list = AppBanner.dao.DTSearchPaginate(dtview.getNowPage(), dtview.getPageSize(), null, sqlext, null,args);
        }//快速查询
        else if(DTGridKit.isFast(dtview)) {
            sqlext += DTGridKit.getQueryConditionByType(dtview, args, DTGridKit.TYPE_FASTQUERY);
            list =AppBanner.dao.DTSearchPaginate(dtview.getNowPage(), dtview.getPageSize(), null, sqlext,null, args);
        }//默认加载
        else {
            Map<String,Object> params= Maps.newHashMap();
            params.put(AppBanner.COL_LIVEFLAG, 1);
            list = AppBanner.dao.searchPaginate(dtview.getNowPage(), dtview.getPageSize(),params);
        }
        dtview=DTGridKit.toDtView(dtview,list);
        renderJson(dtview);
    }
    @Override
    public void delete() {
        String uuid = getPara();
        JsonView json = new JsonView();
        AppBanner ab = AppBanner.dao.findById(uuid);
        boolean status = false;
        if (ab != null) {
            ab.set(AppBanner.COL_LIVEFLAG, "0");
            status = ab.update();
        }
        json.setStatus(status);
        json.setMsg(status ? OperateType.OPERATE_SUCCESS : OperateType.OPERATE_FAIL);
        renderJson(json);
    }
    @Override
    public void add() {
        setAttr("m", new AppBanner());
        render(PathBizKit.getPageAdminPath("system/appbanner/banner_add.ftl"));
    }
    @Override
    public void edit() {
        String uuid = getPara();
        AppBanner ab = AppBanner.dao.findById(uuid);
        setAttr("m", ab);
        render(PathBizKit.getPageAdminPath("system/appbanner/banner_update.ftl"));
    }
    @Override
    public void update() {
        try {
            getFile();
        }
        catch (Exception e) {
            logger.error(e.getLocalizedMessage(), e);
        }
        AppBanner ab = getModel(AppBanner.class, "m");
        boolean status = ab.update();
        JsonView json = new JsonView();
        json.setStatus(status);
        json.setMsg(status ? OperateType.UPDATE_SUCCESS : OperateType.UPDATE_FAIL);
        renderJson(json);
    }
    @Override
    public void save() {
        /**
         *  特别注意:如果客户端请求为 multipart request(form 表单使用了 enctype="multipart/form-data"),
         *  那么必须先调用 getFile 系列方法才 能使 getPara 系列方法正常工作
         *  因为用"multipart/form-data"提交的是二进制数据，需要先解析参数。
         */
        try {
            getFile();
        }
        catch (Exception e) {
            logger.error(e.getLocalizedMessage(), e);
        }
        AppBanner ab = getModel(AppBanner.class, "m");
        boolean status = ab.save();
        JsonView json = new JsonView().setStatus(status);
        json.setMsg(status ? OperateType.SAVE_SUCCESS : OperateType.SAVE_FAIL);
        renderJson(json);
    }
}
